import random
import sys

from jieba.analyse.analyzer import ChineseAnalyzer

from common.models import db
from datetime import datetime
from common.models.user import UserBase
from common.models.base import Base


class CourseType(Base):
    """
    课程类别
    """
    id = db.Column(db.Integer, primary_key=True, doc='课程类型id')
    title = db.Column(db.String(16), doc='课程类别')
    sequence = db.Column(db.Integer, doc='展示顺序', default=10)

    __tablename__ = 'tb_course_type'

    def __str__(self):
        return self.title


class Tag(Base):
    """
    课程标签
    """
    __tablename__ = 'tb_course_tag'
    __searchable__ = ['title']
    __analyzer__ = ChineseAnalyzer()

    id = db.Column(db.Integer, primary_key=True, doc='课程标签id')
    title = db.Column(db.String(16), doc='课程类别')
    sequence = db.Column(db.Integer, doc='展示顺序', default=10)
    course = db.relationship('Course', secondary='course_tag', backref=db.backref('tags'))

    def __str__(self):
        return self.title


class Course(Base):
    """
    课程表
    """
    __tablename__ = 'tb_course'
    __searchable__ = ['title', 'desc']
    __analyzer__ = ChineseAnalyzer()

    STATUS = (
        ('0', '即将上线'),
        ('1', '已上线'),
        ('2', '已下线'),
    )
    id = db.Column(db.Integer, primary_key=True, doc='课程id')
    title = db.Column(db.String(24), doc='课程名称')
    desc = db.Column(db.String(256), doc='课程藐视')
    img_path = db.Column(db.String(256), doc='课程logo地址')
    course_type = db.Column(db.Integer, db.ForeignKey("tb_course_type.id", ondelete="CASCADE"))
    status = db.Column(db.String(8), doc='课程logo地址', default='已上线')
    follower = db.Column(db.Integer, default=0, doc='关注人数')
    learner = db.Column(db.Integer, default=0, doc='学习人数')
    chapters = db.relationship('Chapters', backref='tb_course')

    # def __str__(self):
    #     return self.id

    def to_json(self):
        """
        自定义格式
        """
        data = {
            'name': self.title,
            'collect_num': self.follower,
            'category': self.course_type
        }
        return data


class CourseTag(Base):
    """
    中间表  课程、标签的中间表
    """
    __tablename__ = 'course_tag'
    course_id = db.Column(db.Integer, db.ForeignKey("tb_course.id"), primary_key=True, doc='课程id')
    tag_id = db.Column(db.Integer, db.ForeignKey("tb_course_tag.id"), primary_key=True, doc='标签id')
    is_delete = db.Column(db.Boolean, doc='状态(0存在对应关系;1不存在对应关系)')


class Chapters(Base):
    """
    章节目录
    """
    __tablename__ = 'tb_chapters'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(24), doc='章标题')
    serial_num = db.Column(db.Integer, doc='章序号')
    course = db.Column(db.Integer, db.ForeignKey("tb_course.id", ondelete="CASCADE"), doc='课程')
    sections = db.relationship('Sections', backref='tb_chapters')

    def __str__(self):
        return self.title


class Sections(Base):
    """
    标题目录
    """
    __tablename__ = 'tb_sections'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(24), doc='章标题')
    serial_num = db.Column(db.Integer, doc='章序号')
    chapters = db.Column(db.Integer, db.ForeignKey("tb_chapters.id", ondelete="CASCADE"), doc='课程')
    learn_time = db.Column(db.Integer, doc='学习小时', default=1)
    content = db.Column(db.Text, doc='学习内容')
    video = db.Column(db.String(256), doc='学习视频')
    seq_num = db.Column(db.Integer, doc='序号', default=1)
    like_count = db.Column(db.Integer, doc='点赞次数', default=0)

    def __str__(self):
        return self.title

# class Follow(db.Model):
#     user = models.ForeignKey('user.User', on_delete=models.CASCADE)
#     course = models.ForeignKey(Course, on_delete=models.CASCADE)
#
#     class Meta:
#         db_table = 'tb_follow'
#
#
# class Path(db.Model):
#     title = models.CharField('路径名', max_length=16)
#     img = models.ImageField('路径图片', upload_to='path', null=True)
#     desc = models.CharField('路径描述', max_length=255)
#     user = models.ManyToManyField('user.User', blank=True)
#
#     def course_total(self):
#         count = 0
#         for jd in self.jieduan.all():
#             count = count + jd.courses.count()
#         return count
#
#     class Meta:
#         db_table = 'tb_path'
#
#     def __str__(self):
#         return self.title
#
#
# class Stage(db.Model):
#     title = models.CharField('阶段名', max_length=16)
#     serial_num = models.IntegerField('阶段序号')
#     path = models.ForeignKey(Path, related_name='stage', on_delete=models.SET_NULL, null=True)
#     courses = models.ManyToManyField(Course, blank=True)
#
#     class Meta:
#         db_table = 'tb_jieduan'
#
#     def __str__(self):
#         return "%s-第%s阶段-%s" % (self.path.title, self.serial_num, self.title)
#
#
# class UserCourse(db.Model):
#     """
#     用户购买的课程
#     """
#     user = models.ForeignKey('user.User', on_delete=models.CASCADE, related_name='paycourse')
#     course = models.ForeignKey('course.Course', on_delete=models.CASCADE, related_name='payuser')
#
#     class Meta:
#         db_table = 'tb_usercourse'
#
#     def __str__(self):
#         return "用户:%s, 课程:%s" % (self.user.username, self.course.title)
#
#
# class UserSections(db.Model):
#     user = models.ForeignKey('user.User', on_delete=models.PROTECT, related_name='usersections')
#     course = models.ForeignKey(Course, on_delete=models.PROTECT, related_name='usersections')
#     section = models.ForeignKey(Sections, on_delete=models.PROTECT, related_name='usersections')
#
#     class Meta:
#         db_table = 'tb_usersections'
#
#     def __str__(self):
#         return "用户:%s, 课程:%s, 小节: %s" % (self.user.username, self.course.title, self.sections.title)
#
#
# # 评论
# class Comment(db.Model):
#     course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course_comment')
#     user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='user_comment')
#     content = models.CharField(max_length=500)
#     time =
#     status =
#
#     class Meta:
#         db_table = "tb_comment"
#
#
# # 评论
# #   A: 123
# #   B:A  234
# # 评论的回复
# class CommentReply(db.Model):
#     comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name='reply')
#     user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='comment')
#     to_user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='comment_to')
#     # replay_id = models.IntegerField('评论回复的id', default=None, null=True)
#     # comment_reply = models.ForeignKey('self', on_delete=models.CASCADE, default=None, null=True,
#     #                                   related_name='commentreply')
#     content = models.CharField(max_length=500)
#
#     class Meta:
#         db_table = "tb_commentreply"
#
#
# class CommentSelf(db.Model):
#     course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course_commentself')
#     user = models.ForeignKey('user.User', on_delete=models.SET_NULL, null=True, related_name='user_commentself')
#     content = models.CharField(max_length=500)
#     father = models.ForeignKey('self', on_delete=models.CASCADE, related_name='children', default=None, null=True)
#
#     class Meta:
#         db_table = "tb_commentself"
